package com.sskj.fca.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.fca.R;
import com.sskj.fca.bean.BankerHistoryBean;
import com.sskj.fca.presenter.BankerJackpotHistoryFragmentPresenter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_FRAGMENT_BANKER_JACKPOT_HISTORY)//庄家奖池-历史
public class BankerJackpotHistoryFragment extends BaseFragment<BankerJackpotHistoryFragmentPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<BankerHistoryBean>> mvcHelper;
    @Autowired
    String moneyType;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_banker_jackpot_history;
    }

    @Override
    public BankerJackpotHistoryFragmentPresenter getPresenter() {
        return new BankerJackpotHistoryFragmentPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_banker_jackpot_history, new SlimInjector<BankerHistoryBean>() {
            @Override
            public void onInject(BankerHistoryBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.app_solid));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.transparent));
                }
                injector.text(R.id.tv_type, data.getType().equals("1") ? "注入" : "抽取")
                        .text(R.id.tv_money, data.getMoney() + " " + moneyType)
                        .text(R.id.tv_date, TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(data.getAdd_time()) * 1000))));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<BankerHistoryBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<BankerHistoryBean>)
                        page -> mPresenter.getBnakerHistoryList(moneyType.equals("JC") ? "pb" : "cny", page, 10), 10);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }
}
